package com.roger.tool.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.telephony.TelephonyManager;

public class ImsiUtil {

	public static String getImsi(Context pContext) {
		List<String> imsiList = ImsiUtil.getImsiList(pContext);
		String imsi = null;
		if (imsiList.size() > 0) {
			for (int i = 0; i < imsiList.size(); i++) {
				String imsiForList = imsiList.get(i);
				if (imsiForList != null && imsiForList.length() >= 15) {
					imsi = imsiForList;
					break;
				}
			}
		}
		// 自行生成
		if (imsi == null || imsi.length() < 15) {
			imsi = SharedUtil.getString(pContext, "IMSI_NAME", "Key_IMSI", null);
		}
		if (imsi == null || imsi.length() < 15) {
			imsi = ImsiUtil.createImsi();
			SharedUtil.editString(pContext, "IMSI_NAME", "Key_IMSI", imsi);
		}
		return imsi;
	}

	private static List<String> getImsiList(Context pContext) {
		List<String> imsiList = new ArrayList<String>();
		TelephonyManager telephonyManager = (TelephonyManager) pContext.getSystemService(Context.TELEPHONY_SERVICE);

		String subscriberId_1 = telephonyManager.getSubscriberId();

		Method getSubscriberIdGeminiMethod = null;
		Method getSubscriberIdMethod = null;
		try {
			getSubscriberIdGeminiMethod = TelephonyManager.class.getDeclaredMethod("getSubscriberIdGemini", new Class[] { Integer.TYPE });
		} catch (Exception e) {
//			e.printStackTrace();
		}
		if (getSubscriberIdGeminiMethod == null)
			try {
				getSubscriberIdMethod = TelephonyManager.class.getDeclaredMethod("getSubscriberId", new Class[] { Integer.TYPE });
			} catch (Exception e) {
//				e.printStackTrace();
			}
		try {
			if ((subscriberId_1 != null) && (subscriberId_1.length() > 0)) {
				imsiList.add(subscriberId_1);
			}
			String subscriberId_2;
			if (getSubscriberIdGeminiMethod != null) {
				subscriberId_2 = (String) getSubscriberIdGeminiMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(0) });
				if ((subscriberId_2 != null) && (!subscriberId_2.equals(subscriberId_1))) {
					imsiList.add(subscriberId_2);
				}
				subscriberId_2 = (String) getSubscriberIdGeminiMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(1) });
				if ((subscriberId_2 != null) && (!subscriberId_2.equals(subscriberId_1))) {
					imsiList.add(subscriberId_2);
				}
			} else if (getSubscriberIdMethod != null) {
				subscriberId_2 = (String) getSubscriberIdMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(0) });
				if ((subscriberId_2 != null) && (!subscriberId_2.equals(subscriberId_1))) {
					imsiList.add(subscriberId_2);
				}
				subscriberId_2 = (String) getSubscriberIdMethod.invoke(telephonyManager, new Object[] { Integer.valueOf(1) });
				if ((subscriberId_2 != null) && (!subscriberId_2.equals(subscriberId_1))) {
					imsiList.add(subscriberId_2);
				}
			} else {
				subscriberId_2 = telephonyManager.getSubscriberId();
				if ((subscriberId_2 != null) && (!subscriberId_2.equals(subscriberId_1))) {
					imsiList.add(subscriberId_2);
				}
			}
		} catch (Exception e) {
//			e.printStackTrace();
		}
		return imsiList;
	}

	/**
	 * 目前只生成移动
	 * 
	 * @return
	 */
	private static String createImsi() {
		Random random = new Random();
		StringBuffer imsiBuffer = new StringBuffer("460");
		int randomPhoneType = random.nextInt(10);
		int randomSystem = random.nextInt(10);
		// 移动
		if (randomPhoneType <= 9) {
			if (randomSystem <= 3) {
				imsiBuffer.append("00");
			} else if (randomSystem <= 7) {
				imsiBuffer.append("02");
			} else if (randomSystem <= 9) {
				imsiBuffer.append("07");
			}
		}
		// 联通
		else if (randomPhoneType <= 8) {
			if (randomSystem <= 5) {
				imsiBuffer.append("01");
			} else if (randomSystem <= 9) {
				imsiBuffer.append("06");
			}
		}
		// 电信
		else if (randomPhoneType <= 9) {
			if (randomSystem <= 5) {
				imsiBuffer.append("03");
			} else if (randomSystem <= 9) {
				imsiBuffer.append("05");
			}
		}
		for (int i = 0; i < 10; i++) {
			imsiBuffer.append(random.nextInt(10));
		}
		return imsiBuffer.toString();
	}

}
